<template>
	<div>
		<view style="height: 200rpx;"> s</view>
		<button type="primary" @click="testInitSdk">{{$t('qiyu初始化init方法')}}</button>
		<button type="primary" @click="testSetUserInfo">{{$t('設置用戶信息')}}</button>
		<button type="primary" @click="testOpenService">{{$t('发起会话')}}</button>
		<button type="primary" @click="testLogout">{{$t('退出登录')}}</button>
		<button type="primary" @click="testFinishPageSwitch">{{$t('url跳转时，关闭聊天界面开关')}}</button>
		<button type="primary" @click="testUpdateOptions">{{$t('更新配置')}}</button>
		<button type="primary" @click="testToggleOn">{{$t('消息开关')}}</button>
		<button type="primary" @click="testSendProduct">{{$t('发送商品卡片')}}</button>
		<button type="primary" @click="ios_registerCallBack">{{$t('ios添加回调')}}</button>
		<button type="primary" @click="sendTextMessage">{{$t('发送文本消息的接口')}}</button>
		<button type="primary" @click="android_botEventListener">{{$t('Android服务直达事件监听器')}}</button>
		<button type="primary" @click="getMessageCount">{{$t('获取消息未读数')}}</button>
		<button type="primary" @click="clearUnreadCount">{{$t('清空消息未读数')}}</button>
		<button type="primary" @click="onReceiveMessageCallback">{{$t('收到消息的回调')}}</button>
		<button type="primary" @click="testAddChangeListener">{{$t('Android添加/注销未读消息变化监听')}}</button>
		<button type="primary" @click="onUnreadCountChangedCallback">{{$t('iOS未读消息发生变化的回调')}}</button>
		<button type="primary" @click="photoPermissionCallback">{{$t('iOS相册权限的回调')}}</button>
		<button type="primary" @click="cameraPermissionCallback">{{$t('iOS相机权限的回调')}}</button>
		<button type="primary" @click="microPermissionCallback">{{$t('iOS麦克风权限的回调')}}</button>
		<button type="primary" @click="androidPermissionCallback">{{$t('Android权限的回调')}}</button>
		
	</div>
</template>

<script>
	// 获取 module 
	var qiyuModule = uni.requireNativePlugin("Netease-QiyuModule")
	const modal = uni.requireNativePlugin('modal');
	const appKey = '3e4baa02f20ca8a40fe94a01ffd8a72d';
	export default {
		data() {
			return {
				addChangeListener: true
			}
		},
		onLoad() {
			plus.globalEvent.addEventListener('TestEvent', function(e) {
				modal.toast({
					message: "TestEvent收到：" + e.msg,
					duration: 1.5
				});
			});
		},
		methods: {
			testInitSdk() {
				// 调用同步方法
				qiyuModule.initSdk({
					appKey: appKey,
					appName: "七鱼客服uniapp",
					pkCerName: "PushKit推送证书名；对应云信的pkCername"
				})
			},
			testSetUserInfo() {
				qiyuModule.setUserInfo({
					userId: '12333',
					data: JSON.stringify([{
							"key": "real_name",
							"value": "土豪"
						},
						{
							"key": "mobile_phone",
							"hidden": true,
							"value": "13800000000"
						},
						{
							"key": "email",
							"value": "13800000000@163.com"
						},
						{
							"key": "avatar",
							"value": "https://qiyukf.com/def_avatar.png"
						},
						{
							"index": 0,
							"key": "account",
							"label": "账号",
							"value": "zhangsan",
							"href": "http://example.domain/user/zhangsan"
						},
						{
							"index": 1,
							"key": "sex",
							"label": "性别",
							"value": "先生"
						},
						{
							"index": 5,
							"key": "reg_date",
							"label": "注册日期",
							"value": "2015-11-16"
						},
						{
							"index": 6,
							"key": "last_login",
							"label": "上次登录时间",
							"value": "2015-12-22 15:38:54"
						}
					])
				}, (ret) => {
					if (ret.code == 200) {
						modal.toast({
							message: '成功',
							duration: 1.5
						});
					} else {
						modal.toast({
							message: '失败' + JSON.stringify(ret.errMsg),
							duration: 1.5
						});
					}

					console.log('js打印' + JSON.stringify(ret))
				})
			},
			testOpenService() {
				qiyuModule.openServiceActivity({
					title: '测试界面2',
					source: {
						// Android参考http://qiyukf.com/docs/guide/android/6-%E5%8A%9F%E8%83%BD%E9%85%8D%E7%BD%AE.html#%E5%95%86%E5%93%81%E5%8D%A1%E7%89%87，结构同ConsultSource
						title: 'sse',
						// vipLevel: 1, // 设置用户VIP等级
						// robotId: 2222, //分配机器人
						// staffId: 3444, //分配客服
						// groupId: 345, //客服组id
						// groupTmpId: 455, //分流客服组id
						// robotFirst: false, //是否客服组优先
						//访客头像	
						ios_sendProduct: false,
						productDetail: {
							title: "商品卡片",
							picture: "https://img2.baidu.com/it/u=3202947311,1179654885&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",
							desc: "我是商品卡片的描述",
							url: "www.baidu.com",
							note: "备注信息，可以显示价格，订单号等，字符数要求小于100",
							show: 1,
							sendByUser: true,
							actionText: "发送按钮文案",
							actionTextColorTip: "0xFFDB7093",
							ext: "一般用户不需要填这个字段，这个字段仅供特定用户使用",
							cardType: 0,
							goodsCId: "商品类目ID",
							goodsCName: "商品类目名称",
							goodsId: "商品ID",
							orderId: "订单ID",
							intent: "支持商品卡片带参数给一触即达",
							tags: //标签数据，数组类型
								[
									//自定义商品信息卡片按钮信息
									{
										label: 'label1',
										url: 'url1',
										focusIframe: 'focusIframe1',
										data: 'data1'
									},
									{
										label: 'label2',
										url: 'url2',
										focusIframe: 'focusIframe2',
										data: 'data2'
									}
								],
						}
					},
					//iOS打开界面的方式，push/present,默认push
					openMode: 'push',
					//以下样式配置，请参考说明文档：http://qiyukf.com/docs/guide/ios/5-%E8%87%AA%E5%AE%9A%E4%B9%89%E6%A0%B7%E5%BC%8F.html#%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8
					//仅支持以下配置项，可以只设置部分字段
					// iosUi_config: {
					// 	themeColor:"#3366FF",
					// 	rightItemStyleGrayOrWhite:true,
					// 	showCloseSessionEntry:false,
					// 	showHeadImage:true,
					// 	showTopHeadImage:false,
					// 	customerHeadImageUrl:"https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/28/16eb0a0bbf3c5dc9~tplv-t2oaga2asx-zoom-crop-mark:1956:1956:1956:1101.image",
					// 	//客服头像
					// 	serviceHeadImageUrl:"https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/11/28/16eb0a0bbf3c5dc9~tplv-t2oaga2asx-zoom-crop-mark:1956:1956:1956:1101.image",
					// 	customMessageTextColor:"",
					// 	customMessageHyperLinkColor:"",
					// 	customMessageTextFontSize:16.0,
					// 	isWebFirst:false,
					// 	serviceMessageTextColor:"",
					// 	serviceMessageHyperLinkColor:"",
					// 	serviceMessageTextFontSize:16.0,
					// 	tipMessageTextColor:"",
					// 	tipMessageTextFontSize:12.0,
					// 	bypassDisplayMode:2,//1中间展示，2底部展示
					// 	sessionMessageSpacing:0,
					// 	headMessageSpacing:4,
					// 	messageButtonTextColor:"",
					// 	messageButtonBackColor:"",
					// 	actionButtonTextColor:"",
					// 	actionButtonBorderColor:"",
					// 	inputTextColor:"",
					// 	inputTextFontSize:14,
					// 	inputTextPlaceholder:"",
					// 	showAudioEntry:true,
					// 	showAudioEntryInRobotMode:true,
					// 	showEmoticonEntry:true,
					// 	showImageEntry:true,
					// 	imagePickerColor:"",
					// 	autoShowKeyboard:true,
					// 	bottomMargin:0,
					// 	showShopEntrance:true,
					// 	showSessionListEntrance:true,
					// 	sessionListEntrancePosition:true,
					// 	imagePickerColor:"",
					// 	sessionTipTextFontSize:14,
					// 	sessionTipBackgroundColor:"",
					// },
				});
			},
			testLogout() {
				qiyuModule.logout((ret) => {
					if (ret.success == true) {
						modal.toast({
							message: '成功',
							duration: 1.5
						});
					} else {
						modal.toast({
							message: '失败' + JSON.stringify(ret.errMsg),
							duration: 1.5
						});
					}
				})
			},
			testToggleOn() {
				qiyuModule.toggleNotification(false)
			},
			testFinishPageSwitch() {
				qiyuModule.setFinishPageSwitch(true)
			},
			//需要在setUserInfo之前调用
			testUpdateOptions() {
				qiyuModule.updateOptions({
					isMixSDK: false,
					logSwitch: false,
					isPullMessageFromServer: false,
					uiCustomization: {
						hideLeftAvatar: true,
						inputTextColor: '#ff5500',
						textMsgColorLeft: '#ff5500',
					},
					statusBarNotificationConfig: {
						showBadge: false,
						titleOnlyShowAppName: true,
					}
				}, (url) => {
					modal.toast({
						message: JSON.stringify(url),
						duration: 1.5
					});
					plus.runtime.openWeb(url)
					console.log('js打印' + JSON.stringify(url))
				})
			},
			testSendProduct() {
				qiyuModule.sendProductMessage({
					title: "商品卡片",
					picture: "https://img2.baidu.com/it/u=3202947311,1179654885&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500",
					desc: "我是商品卡片的描述",
					reselectText: "重新选择",
					url: "www.baidu.com",
					note: "备注信息，可以显示价格，订单号等，字符数要求小于100",
					show: 1,
					sendByUser: true,
					actionText: "发送按钮文案",
					actionTextColorTip: "0xFFDB7093",
					ext: "一般用户不需要填这个字段，这个字段仅供特定用户使用",
					cardType: 0,
					goodsCId: "商品类目ID",
					goodsCName: "商品类目名称",
					goodsId: "商品ID",
					orderId: "订单ID",
					intent: "支持商品卡片带参数给一触即达",
					tags: //标签数据，数组类型
						[
							//自定义商品信息卡片按钮信息
							{
								label: 'label1',
								url: 'url1',
								focusIframe: 'focusIframe1',
								data: 'data1'
							},
							{
								label: 'label2',
								url: 'url2',
								focusIframe: 'focusIframe2',
								data: 'data2'
							}
						],
				})
			},
			testAddChangeListener() {
				/**
				 * {
					 * register: true/false 添加监听/注销监听
				 }
				 */
				qiyuModule.addUnreadCountChangeListener({
					register: this.addChangeListener
				}, (ret)=>{
					modal.toast({
						message:"未读数：" + ret,
						duration:1.5
					});
				})
				this.addChangeListener = !this.addChangeListener
			},
			getPlatformObject(ysfOptions) {

				if (uni.getSystemInfoSync().platform == "ios") {
					return {

					}
				} else {
					return ysfOptions
				}
			},
			ios_registerCallBack() {
				//请求客服之前回调
				qiyuModule.requestStaffBeforeBlock((ret) => {
					modal.toast({
						message: '请求客服之前回调',
						duration: 1.5
					});
				});
				// 请求客服后回调
				qiyuModule.requestStaffAfterBlock((ret) => {
					modal.toast({
						message: '请求客服后回调',
						duration: 1.5
					});
				});
				//点击超链接回调
				qiyuModule.linkClick({
					//传1则使用七鱼默认webview打开链接，传0或者不传这个字段，则只有回调，不打开webview，webview的打开交给开发者自行实现
					isUserDefaultWebView: '1'
				}, (ret) => {
					modal.toast({
						message: '点击超链接回调' + JSON.stringify(ret.linkAddress),
						duration: 1.5
					});
				});
				//订单卡片按钮点击事件
				qiyuModule.commodityActionBlock((ret) => {
					modal.toast({
						message: '订单卡片按钮点击事件' + JSON.stringify(ret),
						duration: 1.5
					});
				});
			},


			android_botEventListener() {
				qiyuModule.setBotEventListener((url) => {
					modal.toast({
						message: JSON.stringify(url),
						duration: 1.5
					});
					plus.runtime.openWeb(url)
					console.log('setBotEventListener打印 ' + JSON.stringify(url))
				})
			},

			sendTextMessage() {
				qiyuModule.sendTextMessage({"text":"测试自定义消息","isLocal":true})
			},
			getMessageCount() {
				if (uni.getSystemInfoSync().platform == "ios") {
					modal.toast({
						message:qiyuModule.allUnreadCount(),
						duration:1.5
					});
				} else {
					qiyuModule.allUnreadCount((ret) => {
						modal.toast({
							message: "获取未读的客服消息数: " + ret,
							duration:1.5
						});
					})
				}
			},
			clearUnreadCount() {
				qiyuModule.clearUnreadCount();
			},
			onUnreadCountChangedCallback() {
				qiyuModule.onUnreadCountChangedCallback((ret) => {
					modal.toast({
						message: '未读消息数发生变化' + JSON.stringify(ret.count),
						duration: 1.5
					});
				});
			},
			onReceiveMessageCallback() {
				qiyuModule.onReceiveMessageCallback((ret) => {
					modal.toast({
						message: JSON.stringify(ret),
						duration: 1.5
					});
				})
			},
			/*
			相册权限被拒绝后再次需要使用相册时的回调，不实现此方法默认使用七鱼的效果
			默认文案：请在iPhone的“设置-隐私-照片”选项中，允许访问你的相册。
			*/
			photoPermissionCallback() {
				qiyuModule.photoPermissionCallback((ret) => {
					uni.showModal({
						title: this.$t("提示"),
						content: this.$t("请在iPhone的“设置-隐私-照片”选项中，允许访问你的相册"),
						success: function(res) {
									if (res.confirm) {
									} else {
									}
								}
					});
				})
			},
			/*
			相机权限被拒绝后再次需要使用相机的回调，不实现此方法默认使用七鱼的效果
			默认文案：请在iPhone的“设置-隐私-相机”选项中，允许访问你的相机。
			*/
			cameraPermissionCallback() {
				qiyuModule.cameraPermissionCallback((ret) => {
					uni.showModal({
						title: this.$t("提示"),
						content: this.$t("请在iPhone的“设置-隐私-相机”选项中，允许访问你的相机"),
						success: function(res) {
									if (res.confirm) {
									} else {
									}
								}
					});
				})
			},
			/*
			麦克风权限被拒绝后再次需要使用麦克风时的回调，不实现此方法默认使用七鱼的效果
			默认文案：请在iPhone的“设置-隐私-麦克风”选项中，允许访问你的麦克风。
			*/
			microPermissionCallback() {
				qiyuModule.microPermissionCallback((ret) => {
					uni.showModal({
						title: this.$t("提示"),
						content:this.$t("请在iPhone的“设置-隐私-麦克风”选项中，允许访问你的麦克风"),
						success: function(res) {
									if (res.confirm) {
									} else {
									}
								}
					});
				})
			},
			
			/**
			 * android权限回调处理
			 */
			androidPermissionCallback() {
				qiyuModule.setPermissionCallback((retCallback) => {
					uni.showModal({
						title: this.$t('提示'),
						content: this.$t("权限")+': ' + retCallback,
						success: function(res) {
									if (res.confirm) {
										qiyuModule.onProcessEventSuccess();
									} else {
										qiyuModule.onInterceptEvent();
									}
								}
					});
				})
			}
		}
	}
</script>